# 1. 简易登录
# 2. 随机对战 pvp1
# 3. 好友对战 pvp2
# 4. 对电脑 pve

require File.expand_path('../lib/stjzb_server', __FILE__)

players = {} # login: websocket

EM::WebSocket.start(host: Settings.host, port: Settings.port) do |ws|
  ws.onopen { |handshake|
    ws.send_message_welcome
    puts "#{ws.object_id} opened"
  }

  ws.onclose {
    ws.logout(players)
    puts "#{ws.object_id} closed"
  }

  ws.onmessage { |msg|
    act, val = Oj.load(msg)
    case act
    when 'simple-login' # 简易登录
      ws.simple_login(val, players)
    when 'logout' # 登出
      ws.logout(players)
    when 'pvp1' # 随机对战
      ws.pvp1(players)
    when 'pvp2'
      ws.pvp2(val, players)
    when 'cancel-matching'
      ws.cancel_matching
    when 'escape-pvp'
      ws.escape_pvp
    when 'stjzb'
      ws.stjzb(val)
    end
  }
end
